﻿using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using CodeFirstEntityConfiguration.Models;

namespace CodeFirstEntityConfiguration.Infrastructure
{
    public class PostDb : DbContext
    {
        public DbSet<Post> Posts { get; set; }

        public PostDb()
            : base("PostDb")
        { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new PostConfiguration());
        }

        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    var q = from type in Assembly.GetExecutingAssembly().GetTypes()
        //            where type.IsClass && type.Namespace == "CodeFirstEntityConfiguration.Models"
        //            select type;
        //    q.ToList().ForEach(type => ConfigureModel(modelBuilder, type));            
        //}

        //private static void ConfigureModel(ModelBuilder modelBuilder, dynamic model)
        //{
        //    var entityConfigType = typeof(EntityConfiguration<>).MakeGenericType(new Type[] { model });
        //    dynamic obj = Activator.CreateInstance(entityConfigType);
        //}
    }
}